package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler.BasedBandwidthAttrLinkHandler;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.SuddenBandwidthHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.ztesoft.zsmart.core.log.ZSmartLogger;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author wanghl386
 * @version 1.0
 * @date 2021/8/27 14:20
 */

@Service
@AttrHandler(SuddenBandwidthHandler.class)
public class SudderBandwidthAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    private static final ZSmartLogger LOGGER = ZSmartLogger.getLogger(SudderBandwidthAttrLinkHandler.class);
    @Override
    public void handle(KiteAttr kiteAttr, KiteInst kiteInst, AttrData attrData) {
        try {
            if ("2826".equals(attrData.getServiceOfferId())) {
                // 临时带宽
                KiteAttr tempBandwidth = kiteInst.getAttr("210004933");
                KiteAttr tempBandwidthStart = kiteInst.getAttr("200001825");
                KiteAttr tempBandwidthEnd = kiteInst.getAttr("200001827");
                BasedBandwidthAttrLinkHandler.filterSuddenWidth(kiteInst, tempBandwidth);
                KiteAttr suddenBandwidthAttr = kiteInst.getAttr("210004191");
                String suddenValue = suddenBandwidthAttr.getValue();
                List<KiteAttrValue> attrValues = tempBandwidth.getAttrValues();
                AtomicInteger index = new AtomicInteger(0);

                attrValues.stream().filter(s -> {
                    index.getAndIncrement();
                    return KiteStringUtils.equals(s.getAttrValue(), suddenValue);
                }).findFirst();
                int indexOfSudden = index.get();
                List<KiteAttrValue> kiteAttrValues = attrValues.subList(0, indexOfSudden);
                tempBandwidth.setAttrValues(kiteAttrValues);
                // 清空原有值
                tempBandwidth.setValue(null);
                tempBandwidth.setValueDesc(null);
                tempBandwidthStart.setValue(null);
                tempBandwidthStart.setValueDesc(null);
                tempBandwidthEnd.setValue(null);
                tempBandwidthEnd.setValueDesc(null);
//                boolean flag = kiteAttrValues.stream().anyMatch(kiteAttrValue -> KiteStringUtils.equals(kiteAttrValue.getAttrValue(), attrValues.get(0).getAttrValue()));
//                if(!flag){
//                    tempBandwidth.setValue(attrValues.get(0).getAttrValue());
//                    tempBandwidth.setValueDesc(attrValues.get(0).getAttrValueDesc());
//                }
            }
        } catch (NumberFormatException e) {
            LOGGER.error("handle-->", e, e.getMessage());
        }
    }

    @Override
    public void handleStyle(KiteAttr kiteAttr, KiteInst kiteInst, AttrData attrData) {

    }
}
